

package com.yeyks.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.dal.domain.SysRole;
import com.yeyks.account.dal.domain.SysUserRole;
import com.yeyks.account.service.SysRoleDeptService;
import com.yeyks.account.service.SysRoleMenuService;
import com.yeyks.account.service.SysRoleService;
import com.yeyks.account.service.SysUserRoleService;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.LoginUser;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.PageUtils;
import com.yeyks.common.utils.Result;
import com.yeyks.common.validator.ValidatorUtils;
import com.yeyks.commonReference.pojo.dto.QueryRoleParam;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author administrator
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysRoleDeptService sysRoleDeptService;
	
	/**
	 * 角色列表
	 */
	@Login
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:list")
	public Result list(QueryRoleParam queryRoleParam){

        Map<String, Object> params = new HashMap<>();
		if (null!=queryRoleParam)
		{
			params.put("roleName",queryRoleParam.getRoleName());
			params.put("pageNum",queryRoleParam.getPageNum());
			params.put("pageSize",queryRoleParam.getPageSize());
		}
		PageUtils page = null; //sysRoleService.queryPage(params);

		return Result.ok().put("body", page);
	}
	
	/**
	 * 角色列表
	 */
	@Login
	@RequestMapping(value = "/select", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:select")
	public Result select(@ApiIgnore @LoginUser AccountInfo user, @RequestParam(name = "all",required = false)Integer all, @RequestParam(name = "token",required = true) String token){

		List<SysRole> list = new ArrayList<>();
		if(null==all||1==all||null==user.getDeptId()){ //查全部
			list = sysRoleService.list();
		}else{  //查自己拥有的角色列表,排除掉自身部门经理角色和总公司管理员角色
			list = sysRoleService.querySelfRoles(Long.valueOf(user.getDeptId()),Long.valueOf(user.getDeptId()));
		}
		return Result.ok().put("list", list);
	}
	
	/**
	 * 角色信息
	 */
	@Login
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:info")
	public Result info(@RequestParam(name = "roleId",required = true)Long roleId,@RequestParam(name = "token",required = true) String token){
		SysRole role = sysRoleService.getById(roleId);
		
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);

		//查询角色对应的部门
		List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(new Long[]{roleId});
		role.setDeptIdList(deptIdList);
		
		return Result.ok().put("role", role);
	}
	
	/**
	 * 保存角色
	 */
	//@SysLog("保存角色")
	@Login
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:save")
	public Result save(SysRole role){
		ValidatorUtils.validateEntity(role);
		
		sysRoleService.saveRole(role);
		
		return Result.ok();
	}
	
	/**
	 * 修改角色
	 */
	//@SysLog("修改角色")
	@Login
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:update")
	public Result update(SysRole role){
		ValidatorUtils.validateEntity(role);
		
		sysRoleService.update(role);
		
		return Result.ok();
	}
	
	/**
	 * 删除角色
	 */
	//@SysLog("删除角色")
	@Login
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:delete")
	public Result delete(@RequestParam(name = "roleId",required = false)Long roleId,@RequestParam(name = "token",required = false) String token){
		//List<Long> ids = Splitters.splitToLong(roleIds, Splitters.COMMA);
		QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SysUserRole::getRoleId,roleId);
		int count = sysUserRoleService.count(queryWrapper) ;
		if(count>0)
		{
			throw  new ServiceException("角色下有关联用户，不可删除!");
		}
		Long[] ids = new Long[1];
		ids[0] = roleId;
		sysRoleService.deleteBatch(ids);
		return Result.ok();
	}
}
